<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>png++: reader.hpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">png++
   &#160;<span id="projectnumber">0.2.1</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">reader.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="reader_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2007,2008   Alex Shulgin</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * This file is part of png++ the C++ wrapper for libpng.  PNG++ is free</span>
<a name="l00005"></a>00005 <span class="comment"> * software; the exact copying conditions are as follows:</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<a name="l00008"></a>00008 <span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<a name="l00009"></a>00009 <span class="comment"> *</span>
<a name="l00010"></a>00010 <span class="comment"> * 1. Redistributions of source code must retain the above copyright notice,</span>
<a name="l00011"></a>00011 <span class="comment"> * this list of conditions and the following disclaimer.</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<a name="l00014"></a>00014 <span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
<a name="l00015"></a>00015 <span class="comment"> * documentation and/or other materials provided with the distribution.</span>
<a name="l00016"></a>00016 <span class="comment"> *</span>
<a name="l00017"></a>00017 <span class="comment"> * 3. The name of the author may not be used to endorse or promote products</span>
<a name="l00018"></a>00018 <span class="comment"> * derived from this software without specific prior written permission.</span>
<a name="l00019"></a>00019 <span class="comment"> *</span>
<a name="l00020"></a>00020 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS&#39;&#39; AND ANY EXPRESS OR</span>
<a name="l00021"></a>00021 <span class="comment"> * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span>
<a name="l00022"></a>00022 <span class="comment"> * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN</span>
<a name="l00023"></a>00023 <span class="comment"> * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00024"></a>00024 <span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED</span>
<a name="l00025"></a>00025 <span class="comment"> * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR</span>
<a name="l00026"></a>00026 <span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF</span>
<a name="l00027"></a>00027 <span class="comment"> * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING</span>
<a name="l00028"></a>00028 <span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
<a name="l00029"></a>00029 <span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00030"></a>00030 <span class="comment"> */</span>
<a name="l00031"></a>00031 <span class="preprocessor">#ifndef PNGPP_READER_HPP_INCLUDED</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#define PNGPP_READER_HPP_INCLUDED</span>
<a name="l00033"></a>00033 <span class="preprocessor"></span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &quot;<a class="code" href="io__base_8hpp.html">io_base.hpp</a>&quot;</span>
<a name="l00036"></a>00036 
<a name="l00037"></a>00037 <span class="keyword">namespace </span>png
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039 
<a name="l00064"></a>00064     <span class="keyword">template</span>&lt; <span class="keyword">class</span> istream &gt;
<a name="l00065"></a><a class="code" href="classpng_1_1reader.html">00065</a>     <span class="keyword">class </span><a class="code" href="classpng_1_1reader.html" title="The PNG reader class template. This is the low-level reading interface--use image class or consumer c...">reader</a>
<a name="l00066"></a>00066         : <span class="keyword">public</span> <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>
<a name="l00067"></a>00067     {
<a name="l00068"></a>00068     <span class="keyword">public</span>:
<a name="l00073"></a><a class="code" href="classpng_1_1reader.html#add29db689e5ee6543a6bf68491018521">00073</a>         <span class="keyword">explicit</span> <a class="code" href="classpng_1_1reader.html#add29db689e5ee6543a6bf68491018521" title="Constructs a reader prepared to read PNG image from a stream.">reader</a>(istream&amp; stream)
<a name="l00074"></a>00074             : <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>(png_create_read_struct(PNG_LIBPNG_VER_STRING,
<a name="l00075"></a>00075                                              static_cast&lt; <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* &gt;(this),
<a name="l00076"></a>00076                                              <a class="code" href="classpng_1_1io__base.html#ad01b0dbb31b81fe9d6de1d9ff2f2136e">raise_error</a>,
<a name="l00077"></a>00077                                              0))
<a name="l00078"></a>00078         {
<a name="l00079"></a>00079             png_set_read_fn(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>, &amp; stream, read_data);
<a name="l00080"></a>00080         }
<a name="l00081"></a>00081 
<a name="l00082"></a><a class="code" href="classpng_1_1reader.html#a877cbc18291159ca94b1acbc224f3ef4">00082</a>         <a class="code" href="classpng_1_1reader.html#a877cbc18291159ca94b1acbc224f3ef4">~reader</a>()
<a name="l00083"></a>00083         {
<a name="l00084"></a>00084             png_destroy_read_struct(&amp; <a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>,
<a name="l00085"></a>00085                                     <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info__base.html#afe70c0d333349a9b5fc7a1d046fc3fbc">get_png_info_ptr</a>(),
<a name="l00086"></a>00086                                     <a class="code" href="classpng_1_1io__base.html#a4dc235d3aa8764d1a1a73c6d1fefed61">m_end_info</a>.<a class="code" href="classpng_1_1info__base.html#afe70c0d333349a9b5fc7a1d046fc3fbc">get_png_info_ptr</a>());
<a name="l00087"></a>00087         }
<a name="l00088"></a>00088 
<a name="l00093"></a><a class="code" href="classpng_1_1reader.html#a426065ee445481d599162d0e22c97214">00093</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1reader.html#a426065ee445481d599162d0e22c97214" title="Reads the whole PNG data stream into memory. Not particularly useful.">read_png</a>()
<a name="l00094"></a>00094         {
<a name="l00095"></a>00095             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00096"></a>00096             {
<a name="l00097"></a>00097                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00098"></a>00098             }
<a name="l00099"></a>00099             png_read_png(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>,
<a name="l00100"></a>00100                          <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info__base.html#a030a8262a2ef1bf8c97a2eafe71c2f3c">get_png_info</a>(),
<a name="l00101"></a>00101                          <span class="comment">/* transforms = */</span> 0,
<a name="l00102"></a>00102                          <span class="comment">/* params = */</span> 0);
<a name="l00103"></a>00103         }
<a name="l00104"></a>00104 
<a name="l00108"></a><a class="code" href="classpng_1_1reader.html#a5dc827c5e9a113f8821c1ff08b2ab111">00108</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1reader.html#a5dc827c5e9a113f8821c1ff08b2ab111" title="Reads info about PNG image.">read_info</a>()
<a name="l00109"></a>00109         {
<a name="l00110"></a>00110             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00111"></a>00111             {
<a name="l00112"></a>00112                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00113"></a>00113             }
<a name="l00114"></a>00114             <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info.html#a8788080158fc7450911516c23d4b584c">read</a>();
<a name="l00115"></a>00115         }
<a name="l00116"></a>00116 
<a name="l00120"></a><a class="code" href="classpng_1_1reader.html#a07f8235aab8059260b4857feba579ccb">00120</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1reader.html#a07f8235aab8059260b4857feba579ccb" title="Reads a row of image data at a time.">read_row</a>(<a class="code" href="namespacepng.html#ac0e0e2e09bdd9a287618bfdc5f31ca52">byte</a>* bytes)
<a name="l00121"></a>00121         {
<a name="l00122"></a>00122             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00123"></a>00123             {
<a name="l00124"></a>00124                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00125"></a>00125             }
<a name="l00126"></a>00126             png_read_row(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>, bytes, 0);
<a name="l00127"></a>00127         }
<a name="l00128"></a>00128 
<a name="l00132"></a><a class="code" href="classpng_1_1reader.html#a198c8345b97ddfac9f2f7474f196f45c">00132</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1reader.html#a198c8345b97ddfac9f2f7474f196f45c" title="Reads ending info about PNG image.">read_end_info</a>()
<a name="l00133"></a>00133         {
<a name="l00134"></a>00134             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00135"></a>00135             {
<a name="l00136"></a>00136                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00137"></a>00137             }
<a name="l00138"></a>00138             <a class="code" href="classpng_1_1io__base.html#a4dc235d3aa8764d1a1a73c6d1fefed61">m_end_info</a>.<a class="code" href="classpng_1_1end__info.html#a6ea148edf8c9d0394d055bfe34461524">read</a>();
<a name="l00139"></a>00139         }
<a name="l00140"></a>00140 
<a name="l00141"></a><a class="code" href="classpng_1_1reader.html#ae38d4ce06488e1addb6e5ff0c8e54a58">00141</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1reader.html#ae38d4ce06488e1addb6e5ff0c8e54a58">update_info</a>()
<a name="l00142"></a>00142         {
<a name="l00143"></a>00143             <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info.html#a544c5e25c4211ef33976e3ac2b2ea020">update</a>();
<a name="l00144"></a>00144         }
<a name="l00145"></a>00145 
<a name="l00146"></a>00146     <span class="keyword">private</span>:
<a name="l00147"></a>00147         <span class="keyword">static</span> <span class="keywordtype">void</span> read_data(png_struct* png, <a class="code" href="namespacepng.html#ac0e0e2e09bdd9a287618bfdc5f31ca52">byte</a>* data, <span class="keywordtype">size_t</span> length)
<a name="l00148"></a>00148         {
<a name="l00149"></a>00149             <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* io = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* <span class="keyword">&gt;</span>(png_get_error_ptr(png));
<a name="l00150"></a>00150             <a class="code" href="classpng_1_1reader.html" title="The PNG reader class template. This is the low-level reading interface--use image class or consumer c...">reader</a>* rd = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1reader.html" title="The PNG reader class template. This is the low-level reading interface--use image class or consumer c...">reader</a>* <span class="keyword">&gt;</span>(io);
<a name="l00151"></a>00151             rd-&gt;<a class="code" href="classpng_1_1io__base.html#afba999be609454b12e2cd4ef3ca9a3f2">reset_error</a>();
<a name="l00152"></a>00152             istream* stream = <span class="keyword">reinterpret_cast&lt;</span> istream* <span class="keyword">&gt;</span>(png_get_io_ptr(png));
<a name="l00153"></a>00153             <span class="keywordflow">try</span>
<a name="l00154"></a>00154             {
<a name="l00155"></a>00155                 stream-&gt;read(reinterpret_cast&lt; char* &gt;(data), length);
<a name="l00156"></a>00156                 <span class="keywordflow">if</span> (!stream-&gt;good())
<a name="l00157"></a>00157                 {
<a name="l00158"></a>00158                     rd-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(<span class="stringliteral">&quot;istream::read() failed&quot;</span>);
<a name="l00159"></a>00159                 }
<a name="l00160"></a>00160             }
<a name="l00161"></a>00161             <span class="keywordflow">catch</span> (std::exception <span class="keyword">const</span>&amp; error)
<a name="l00162"></a>00162             {
<a name="l00163"></a>00163                 rd-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(error.what());
<a name="l00164"></a>00164             }
<a name="l00165"></a>00165             <span class="keywordflow">catch</span> (...)
<a name="l00166"></a>00166             {
<a name="l00167"></a>00167                 assert(!<span class="stringliteral">&quot;read_data: caught something wrong&quot;</span>);
<a name="l00168"></a>00168                 rd-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(<span class="stringliteral">&quot;read_data: caught something wrong&quot;</span>);
<a name="l00169"></a>00169             }
<a name="l00170"></a>00170             <span class="keywordflow">if</span> (rd-&gt;<a class="code" href="classpng_1_1io__base.html#a4bfbb79bd63e35d54339f15baade71f5">is_error</a>())
<a name="l00171"></a>00171             {
<a name="l00172"></a>00172                 rd-&gt;<a class="code" href="classpng_1_1io__base.html#ad01b0dbb31b81fe9d6de1d9ff2f2136e">raise_error</a>();
<a name="l00173"></a>00173             }
<a name="l00174"></a>00174         }
<a name="l00175"></a>00175     };
<a name="l00176"></a>00176 
<a name="l00177"></a>00177 } <span class="comment">// namespace png</span>
<a name="l00178"></a>00178 
<a name="l00179"></a>00179 <span class="preprocessor">#endif // PNGPP_READER_HPP_INCLUDED</span>
</pre></div></div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated on Fri Jan 25 2013 14:10:10 for png++ by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
